<?php
namespace Admin\Controller;
use \Think\Controller;

class PublicController extends Controller{
	public function dologin(){
		$username = I('post.username',0);
		$password = I('post.password',0);
		$model = M('adminuser');
          
    
		
		$data=$model->where(array('username'=>$username))->find();
	 
	

		if(is_null($data)){
			$this->error('账号不存在！');
			exit;
		}
		if($data['userpass'] !=md5($password)){
			$this->error('密码不正确，请重新输入！');
			exit;
		}
		$_SESSION['admin_user'] = $data;
		//根据用户id获取对应的节点信息
		//$sql = "select n.mname,n.aname from lamp_user u join lamp_user_role ur on u.id=ur.uid join lamp_role_node rn on ur.rid=rn.rid join lamp_node n on rn.nid=n.id where u.id={$users['id']}";
		//$list = M()->query($sql);
		$list = M('node')->field('mname,aname')->where('id in'.M('role_node')->field('nid')->where("rid in ".M('user_role')->field('rid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->select();
		$nodelist = array();
		$nodelist['Index'] = array('index');
		// dump($list);
		// dump($nodelist);
		//遍历重新拼装
		foreach($list as $v){
			$nodelist[$v['mname']][] = $v['aname'];
		
			if($v['aname']=="edit"){
				$nodelist[$v['mname']][]="save";
			}
			if($v['aname']=="add"){
				$nodelist[$v['mname']][]="doadd";
			}
		}
		//将权限信息放置到session中
		$_SESSION['admin_user']['nodelist'] = $nodelist;
		// dump($_SESSION);exit;
		//成功跳转
		$this->redirect('Index/index', '', 3, 'Login ....');
	}
	public function loginout(){
		unset($_SESSION['admin_user']);
		$this->redirect('Public/login');
	}
}
